package com.manbearpig.todue;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//import android.util.Log;
import java.util.regex.*;

/**
 * The User class validates user data both inside and outside of the database.
 * @author Brandon Devereaux
 * @version 1.0
 *
 */
public class User {
	private String name;
	private String email;
	private String username;
	private String password;
	
	/**
	 * An enumeration for supporting invalid field errors
	 * @author Brandon Devereaux
	 *
	 */
	public enum invalidField
	{
		EMAIL,USERNAME,NONE
	}
	
	/**
	 * Instantiate the User class with all fields
	 * @param name The requested name
	 * @param email The requested email
	 * @param username The requested username
	 * @param password The requested password
	 */
	public User(String name, String email, String username, String password)
	{
		this.name = name;
		this.email = email;
		this.username = username;
		this.password = password;
	}
	
	/**
	 * Instantiate a user class to test only username and password
	 * @param username The requested username
	 * @param password The requested password
	 */
	public User(String username, String password)
	{
		this("","",username, password);
	}
	
	/**
	 * This method checks whether the entered username and password are valid
	 * @param c The Context needed for the DatabaseHelper
	 * @return a boolean representing whether the username and password are valid
	 */
	public boolean validateLogin(Context c)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
		SQLiteDatabase db = dbh.getReadableDatabase();
		String sql = "SELECT * FROM USERS WHERE Username=? AND Password=?";
		Cursor cursor = db.rawQuery(sql, new String[]{this.username,this.password});
		if(cursor.getCount()>0)//1 row means username and password are valid
		{
			cursor.close();
			db.close();
			dbh.close();
			return true;
		}
		else
		{
			cursor.close();
			db.close();
			dbh.close();
			return false;
		}
	}
	
	
	/**
	 * This method checks whether the new user information is valid
	 * @param c The context we ultimately pass to our DatabaseHelper
	 * @return a boolean representing whether all new user information is valid
	 */
	public invalidField validateAll(Context c)
	{
		if(exists(c))
			return invalidField.USERNAME;
		else
		{ 
			String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
	  	    CharSequence inputStr = this.email;  
			Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
			Matcher matcher = pattern.matcher(inputStr);  
			if(matcher.matches()==false)
				return invalidField.EMAIL;
		}
		return invalidField.NONE;
	}
	
	/**
	 * This method checks to see if a certain username already exists in the database
	 * @param c The Context we ultimately pass to our DatabaseHelper
	 * @return True if the username already exists, False otherwise
	 */
	private boolean exists(Context c)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
		SQLiteDatabase db = dbh.getReadableDatabase();
		String sql = "SELECT * FROM Users WHERE Username=?";
		Cursor cursor = db.rawQuery(sql, new String[]{this.username});
		if(cursor.getCount()>0)//1 row means username already exists
		{
			cursor.close();
			db.close();
			dbh.close();
			return true;
		}
		else
		{
			cursor.close();
			db.close();
			dbh.close();
			return false;
		}
	}
	
	/**
	 * This method stores new user credentials in the database
	 * @param c The Context we ultimately pass to our DatabaseHelper
	 * @return returns true if the credentials were successfully stored, false otherwise
	 */
	public void storeUserCredentials(Context c)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
		SQLiteDatabase db = dbh.getWritableDatabase();
		db.beginTransaction();
		ContentValues cv = new ContentValues();
		cv.put("Username", this.username);
		cv.put("Name", this.name);
		cv.put("Email", this.email);
		cv.put("Password", this.password);
		db.insert("Users", null, cv);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
		dbh.close();
	}
	
	/**
	 * This method edits existing user credentials in the database
	 * @return returns true if the credentials were successfully edited and saved, false otherwise
	 */
	public boolean editUserCredentials()
	{
		return false;
	}
}
